# -*- coding: utf_8 -*-
# @Create   : 2021/8/17 11:24
# @Author   : yh
# @Remark   : 视频组管理
from mxsoftpy import View

from server.common.VideoManage.VideoQuality.video import VideoServer
from server.common.VideoManage.VideoQuality.video_group import VideoGroupServer

from utils.public.params_method import check_params, pro_decode


class VideoGroupList(View):

    def post(self):
        """
        展示视频组列表
        """
        check_params(self.request.POST, ['s_code'])
        return '查询成功', VideoGroupServer().list(**self.request.POST)


class VideoGroupAdd(View):

    def post(self):
        """
        添加视频组
        """
        check_params(self.request.POST, ['name', 'parent_code'])
        param = pro_decode(**self.request.POST)
        return '添加视频组成功', VideoGroupServer().create(**param)


class VideoGroupDelete(View):

    def post(self):
        """
        删除视频组，支持单个code或者批量删除
        """
        check_params(self.request.POST, ['group_code'])
        video_list = VideoServer().select(name='', group_code=self.request.POST['group_code'])
        if video_list['data']:
            return '该分组下存在视频源，请删除或移动后重试'

        return '删除视频组成功', {'group_code': VideoGroupServer().delete(**self.request.POST)}


class VideoGroupUpdate(View):

    def post(self):
        """
        更新视频组名称
        """
        check_params(self.request.POST, ['group_code', 'name'])
        if not isinstance(self.request.POST['name'], str):
            return '错误的名称类型: %s, 需要的类型: str' % type(self.request.POST['name']).__name__
        param = pro_decode(**self.request.POST)
        return '更新成功', VideoGroupServer().update(**param)


class VideoGroupSelect(View):

    def post(self):
        """
        根据关键词查询视频分组
        """
        check_params(self.request.POST, ['name'])
        param = pro_decode(**self.request.POST)
        return '查询成功', VideoGroupServer().select(**param)
